package cn.jvtc.jx.a0603laihongrong;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;


import java.util.ArrayList;
import java.util.List;

import cn.jvtc.jx.a0603laihongrong.Adapter.ViewPageAdapter;
import cn.jvtc.jx.a0603laihongrong.Fragment.HelpFragment;
import cn.jvtc.jx.a0603laihongrong.Fragment.MimeFragment;
import cn.jvtc.jx.a0603laihongrong.Fragment.SetFragment;
import cn.jvtc.jx.a0603laihongrong.Fragment.UserFragment;
import cn.jvtc.jx.a0603laihongrong.pojo.User;


public class LoginSuccessActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    TextView tv;
    User user;
    RadioGroup rb;
    UserFragment userFragment;
    SetFragment setFragment;
    HelpFragment helpFragment;
    MimeFragment mimeFragment;
    List<Fragment> fragmentList;
    ViewPager viewPager;
    ViewPageAdapter adapter;
    RadioButton users,set,help,mime;
    String[] title=new String[]{"主页","设置","帮助","我的"};
    private static final String TAG = "LoginSuccessActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_success);
        initView();
        tv.setText("主页");
        Intent intent=getIntent();
        user = (User) intent.getSerializableExtra("user");
        Log.d(TAG, "onCreate: "+user);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

            @Override
            public void onPageSelected(int position) {
                tv.setText(title[position]);
                switch (position){
                    case 0:
                        users.setChecked(true);
                        break;
                    case 1:
                        set.setChecked(true);
                        break;
                    case 2:
                        help.setChecked(true);
                        break;
                    case 3:
                        mime.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) { }
        });
    }
    public void initView(){
        tv=findViewById(R.id.tv);
        rb=findViewById(R.id.rb);
        rb.setOnCheckedChangeListener(this);
        users=findViewById(R.id.home);
        help=findViewById(R.id.help);
        set=findViewById(R.id.set);
        mime=findViewById(R.id.mime);
        viewPager=findViewById(R.id.fragment);
        userFragment=new UserFragment();
        setFragment=new SetFragment();
        helpFragment=new HelpFragment();
        mimeFragment=new MimeFragment();
        fragmentList=new ArrayList<>();
        fragmentList.add(userFragment);
        fragmentList.add(setFragment);
        fragmentList.add(helpFragment);
        fragmentList.add(mimeFragment);
        adapter=new ViewPageAdapter(getSupportFragmentManager(),0,fragmentList);
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0);
    }

    public User send(){
        return user;
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.home:
                viewPager.setCurrentItem(0);
                break;
            case R.id.set:
                viewPager.setCurrentItem(1);
                break;
            case R.id.help:
                viewPager.setCurrentItem(2);
                break;
            case R.id.mime:
                viewPager.setCurrentItem(3);
                break;
        }
    }

}
